/*
 * Copyright 2008 - 2010 Lars Heuer (heuer[at]semagia.com). All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.semagia.atomico.dm.impl;

import com.semagia.atomico.dm.IAuthor;

/**
 * Immutable {@link IAuthor} implementation.
 * 
 * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a>
 */
public final class Author implements IAuthor {

    private final String _name;
    private final String _email;
    private final String _iri;

    /**
     * Creates an IAuthor with the name '[unknown]'.
     */
    public Author() {
        this("[unknown]");
    }

    /**
     * Creates an author with the provided name.
     *
     * @param name The name of the author.
     */
    public Author(final String name) {
        this(name, null, null);
    }

    /**
     * Creates an author with the provided name and e-mail address.
     *
     * @param name The name of the author.
     * @param email The e-mail address of the author or {@code null}.
     */
    public Author(final String name, final String email) {
        this(name, email, null);
    }

    /**
     * Creates an author with the provided name, e-mail address, and IRI.
     *
     * @param name The name of the author.
     * @param email The e-mail address of the author or {@code null}.
     * @param email The IRI of the author or {@code null}.
     */
    public Author(final String name, final String email, final String iri) {
        if (name == null || name.isEmpty()) {
            throw new IllegalArgumentException("The author's name must not be null or empty");
        }
        if (email != null && email.isEmpty()) {
            throw new IllegalArgumentException("The author's e-mail must not be empty");
        }
        if (iri != null && iri.isEmpty()) {
            throw new IllegalArgumentException("The author's IRI must not be empty");
        }
        _name = name;
        _email = email;
        _iri = iri;
    }

    /* (non-Javadoc)
     * @see com.semagia.atomico.dm.IAuthor#getName()
     */
    @Override
    public String getName() {
        return _name;
    }

    /* (non-Javadoc)
     * @see com.semagia.atomico.dm.IAuthor#getEmail()
     */
    @Override
    public String getEmail() {
        return _email;
    }

    /* (non-Javadoc)
     * @see com.semagia.atomico.dm.IAuthor#getIRI()
     */
    @Override
    public String getIRI() {
        return _iri;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof IAuthor)) {
            return false;
        }
        final IAuthor other = (IAuthor) obj;
        return _name.equals(other.getName()) 
                && (_email == null ? other.getEmail() == null
                                   : _email.equals(other.getEmail()))
                && (_iri == null ? other.getIRI() == null
                                 : _iri.equals(other.getIRI()));
    }

    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        int result = _name.hashCode();
        if (_email != null) {
            result += 31 * _email.hashCode();
        }
        if (_iri != null) {
            result += 31 * _iri.hashCode();
        }
        return result;
    }

}
